//
//  ShopMainViewController.m
//  BaseFrame
//
//  Created by wingfac on 2017/8/9.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "ShopMainPageViewController.h"
#import "ShopMainPageCellLeftTableViewCell.h"
#import "ShopMainPageCellRightTableViewCell.h"
#define groupBuyingBtnX (SCREEN_WIDTH/2 - btnW)/2

@interface ShopMainPageViewController ()<UITableViewDelegate,UITableViewDataSource>

//@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIImageView *headerImgView;
@property (nonatomic,strong) UIView * headerView;//tableView头
@property (nonatomic,strong) UIButton * groupBuyingBtn;//团购
@property (nonatomic,strong) UIButton * takeOutBtn;//外卖
@property (nonatomic,strong) UIView * sliderView;//按钮下滑动条
@property (nonatomic,assign) BOOL isGroupClick;//判断是否为团购
@property (nonatomic,strong) UITableView * leftTableView;//
@property (nonatomic,strong) UITableView * rightTableView;//

Strong NSMutableArray *dataArray; /**<*/

@end

//网络
@implementation ShopMainPageViewController (NetWorking)
///**
// 修改图片
// */
//- (void)getmodifyShopAdvertisingMap{
//    [self showprogressHUD];
//    NSDictionary *parameters = @{@"s_id":[UserModel sharedInstanced].s_id,
//                                 };
//    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/Store_Mobile/modifyShopAdvertisingMap.action" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//
//        NSData *data = UIImageJPEGRepresentation(self.headerImgView.image, .1);
//        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
//        [formData appendPartWithFileData:data name:@"file" fileName:[NSString stringWithFormat:@"%ld.jpg",(NSUInteger)time] mimeType:@"image/jpeg"];
//
//    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        ShowMessage(responseObject[@"msg"]);
//        [self hiddenProgressHUD];
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [self showError:error];
//        [self hiddenProgressHUD];
//    }];
//}
//
//- (void)getData{
//    [self showprogressHUD];
//    NSDictionary *parameters = @{@"id":[UserModel sharedInstanced].userId};
//    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/Store_Mobile/enterTheShopAdvertisingChartInterface.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        [self hiddenProgressHUD];
//        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
//            for (int i = 0 ; i < [responseObject[@"obj1"] count]; i ++) {
//                NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:responseObject[@"obj1"][i]];
//
//                [self.dataArray addObject:dic];
//            }
//            [self.headerImgView sd_setImageWithURL:[NSURL URLOffsetIPWithString:responseObject[@"obj"]] placeholderImage:[UIImage imageNamed:@"button_mapadd"]];
//            if ([responseObject[@"obj"] isEqualToString:@" "]) {
//                self.headerImgView.contentMode = UIViewContentModeCenter;
//            }else{
//                self.headerImgView.contentMode = UIViewContentModeScaleAspectFill;
//            }
////            [self.view addSubview:self.tableView];
//        }else{
//            ShowMessage(responseObject[@"msg"]);
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [self hiddenProgressHUD];
//        [self showError:error];
//    }];
//}
//
//- (void)getModifyCommodityCategories:(NSDictionary *)dic{
//    [self showprogressHUD];
//    NSDictionary *parameters = @{@"cc_id":dic[@"cc_id"],
//                                 @"cc_name":dic[@"cc_name"]};
//    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/Commodity_Category/modifyCommodityCategories.action" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        if ([dic[@"cc_picture"] isKindOfClass:[UIImage class]]) {
//            NSData *data = UIImageJPEGRepresentation(dic[@"cc_picture"], .1);
//            NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
//            [formData appendPartWithFileData:data name:@"file" fileName:[NSString stringWithFormat:@"%ld.jpg",(NSUInteger)time] mimeType:@"image/jpeg"];
//        }
//
//    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        //        if (State(responseObject)) {
//        //        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
//        //            [self.view makeToast:responseObject[@"msg"] duration:1 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
//        //                [self.navigationController popViewControllerAnimated:YES];
//        //            }];
//        //        }else{
//        ShowMessage(responseObject[@"msg"]);
//        //        }
//        [self hiddenProgressHUD];
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [self showError:error];
//        [self hiddenProgressHUD];
//    }];
//
//}

@end

@implementation ShopMainPageViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"店铺信息";
//    [self getData];
    
    UIButton * rightButton = [[UIButton alloc]init];
    rightButton.size = CGSizeMake(40, 20);
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    [rightButton.titleLabel setFont:DEF_FontSize_11];
    rightButton.layer.masksToBounds = YES;
    rightButton.layer.cornerRadius = 3;
    rightButton.layer.borderColor = [[UIColor whiteColor] CGColor];
    rightButton.layer.borderWidth = 1;
    [rightButton addTarget:self action:@selector(rightBarButtonItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    //设置barbutton
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self.view addSubview:self.leftTableView];
    [self.view addSubview:self.rightTableView];
    [self.view addSubview:self.headerView];
}

#pragma mark - Event
- (void)typeBtnClick:(UIButton *)sender{
    if (sender.frame.origin.x + 1 > SCREEN_WIDTH/2 ) {
        self.isGroupClick = NO;
        self.groupBuyingBtn.selected = NO;
        self.takeOutBtn.selected = YES;
    }else{
        self.isGroupClick = YES;
        self.groupBuyingBtn.selected = YES;
        self.takeOutBtn.selected = NO;
    }
    __weak typeof (self)weak = self;
    [UIView animateWithDuration:0.3 animations:^{
        weak.sliderView.x = sender.x;
    }];
}

- (void)leftBarButtonItemClick{
    
}

- (void)rightBarButtonItemClick{
    [self.view endEditing:YES];
//    [self getmodifyShopAdvertisingMap];
    for (int i = 0; i < self.dataArray.count; i ++) {
        NSDictionary *dic = self.dataArray[i];
        if (![dic[@"cc_name"] isEqualToString:@" "]) {
//            [self getModifyCommodityCategories:dic];
        }
    }
}

- (void)tableHeadViewCkick{
    [self selectPhotoAlbumWithSelectPhotoHandle:^(UIImage *img) {
        self.headerImgView.image = img;
        self.headerImgView.contentMode = UIViewContentModeScaleAspectFill;
        //        state = YES;
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.dataArray.count;
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
        ShopMainPageCellLeftTableViewCell *cell = [ShopMainPageCellLeftTableViewCell cellForTableView:tableView];
        return cell;
    }
    ShopMainPageCellRightTableViewCell *cell = [ShopMainPageCellRightTableViewCell cellForTableView:tableView];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 58 / WIDTH_5S_SCALE;
}

#pragma mark - set & get
- (UIImageView *)headerImgView{
    if (!_headerImgView) {
        _headerImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 130/WIDTH_5S_SCALE)];
        _headerImgView.image = [UIImage imageNamed:@"button_mapadd"];
        _headerImgView.backgroundColor = getColor(@"f1f2f6");
        _headerImgView.contentMode = UIViewContentModeCenter;
        _headerImgView.layer.masksToBounds = YES;
        _headerImgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tableHeadViewCkick)];
        [_headerImgView addGestureRecognizer:tap];
    }
    return _headerImgView;
}

- (UIView *)headerView{
    if (!_headerView) {
        int btnH = 36/WIDTH_5S_SCALE;
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.headerImgView.height + btnH)];
        _headerView.backgroundColor = [UIColor clearColor];
        [_headerView addSubview:self.headerImgView];
        //团购
        int btnW = 50/WIDTH_5S_SCALE;
        UIButton * groupBuyingBtn = [[UIButton alloc] initWithFrame:CGRectMake(groupBuyingBtnX, self.headerImgView.bottom, btnW, 36/WIDTH_5S_SCALE)];
        [groupBuyingBtn setTitle:@"团购" forState:UIControlStateNormal];
        [groupBuyingBtn setTitleColor:getColor(mainColor) forState:UIControlStateSelected];
        [groupBuyingBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [groupBuyingBtn addTarget:self action:@selector(typeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:groupBuyingBtn];
        groupBuyingBtn.selected = YES;
        self.groupBuyingBtn = groupBuyingBtn;
        //外卖
        UIButton * takeOutBtn = [[UIButton alloc] initWithFrame:CGRectMake(groupBuyingBtnX + SCREEN_WIDTH/2, groupBuyingBtn.top, btnW, 36/WIDTH_5S_SCALE)];
        [takeOutBtn setTitle:@"外卖" forState:UIControlStateNormal];
        [takeOutBtn setTitleColor:getColor(mainColor) forState:UIControlStateSelected];
        [takeOutBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [takeOutBtn addTarget:self action:@selector(typeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:takeOutBtn];
        self.takeOutBtn = takeOutBtn;
        //线
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, groupBuyingBtn.bottom, SCREEN_WIDTH, 1)];
        lineView.backgroundColor = getColor(@"eeeeee");
        [_headerView addSubview:lineView];
        self.sliderView = [[UIView alloc] initWithFrame:CGRectMake(groupBuyingBtn.left, lineView.top - 1, 50/WIDTH_5S_SCALE, 2)];
        self.sliderView.backgroundColor = getColor(mainColor);
        [_headerView addSubview:self.sliderView];
    }return _headerView;
}

- (UITableView *)leftTableView{
    if (!_leftTableView) {
        _leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.headerView.bottom, 94 / WIDTH_5S_SCALE, SCREEN_HEIGHT - 64 - 150 / WIDTH_5S_SCALE - 50 / WIDTH_5S_SCALE) style:UITableViewStyleGrouped];
        _leftTableView.delegate = self;
        _leftTableView.dataSource = self;
        _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _leftTableView.sectionFooterHeight = .01;
        _leftTableView.sectionHeaderHeight = .01;
        _leftTableView.tableFooterView = [UIView new];
        _leftTableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _leftTableView.showsVerticalScrollIndicator = NO;
        _leftTableView.showsHorizontalScrollIndicator = NO;
        _leftTableView.backgroundColor = getColor(@"ffffff");
//        [_leftTableView registerClass:[ShopMainPageCellLeftTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ShopMainPageCellLeftTableViewCell class])];

    }
    return _leftTableView;
}

- (UITableView *)rightTableView{
    if (!_rightTableView) {
        _rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(self.leftTableView.right, self.leftTableView.top, SCREEN_WIDTH - 94 / WIDTH_5S_SCALE, SCREEN_HEIGHT - 64 - 150 / WIDTH_5S_SCALE - 50 / WIDTH_5S_SCALE) style:UITableViewStyleGrouped];
        _rightTableView.delegate = self;
        _rightTableView.dataSource = self;
        _rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _rightTableView.sectionFooterHeight = .01;
        _rightTableView.sectionHeaderHeight = .01;
        _rightTableView.tableFooterView = [UIView new];
        _rightTableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _rightTableView.showsVerticalScrollIndicator = NO;
        _rightTableView.showsHorizontalScrollIndicator = NO;
        _rightTableView.backgroundColor = getColor(@"ffffff");
    }
    return _rightTableView;
}
#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[
                        ].mutableCopy;
    }
    return _dataArray;
}


@end
